package com.rain.controller;

import com.mybatisflex.core.paginate.Page;
import com.rain.annotation.AuthCheck;
import com.rain.common.BaseResponse;
import com.rain.common.DeleteRequest;
import com.rain.common.ResultUtils;
import com.rain.constant.UserConstant;
import com.rain.dto.user.*;
import com.rain.exception.ErrorCode;
import com.rain.exception.ThrowUtils;
import com.rain.entity.User;
import com.rain.vo.LoginUserVo;
import com.rain.vo.UserVo;
import com.rain.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  控制层。
 *
 * @author <a href="https://github.com/liyupi">程序员刘诗雨</a>
 */
@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("register")
    BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        ThrowUtils.throwIf(userRegisterRequest==null, ErrorCode.PARAMS_ERROR);
        long userRegister = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(userRegister);
    }

    @PostMapping("login")
    BaseResponse<LoginUserVo> userlogin(@RequestBody UserLoginRequest userLoginRequest
    , HttpServletRequest request){
        ThrowUtils.throwIf(userLoginRequest==null, ErrorCode.PARAMS_ERROR);
        LoginUserVo loginUserVo=userService.userlogin(userLoginRequest,request);
        return ResultUtils.success(loginUserVo);
    }

    @GetMapping("getLoginUser")
    BaseResponse<User> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(loginUser);
    }

    @PostMapping("loginOut")
    BaseResponse<Boolean> loginOut(HttpServletRequest request){
        Boolean res= userService.loginOut(request);
        return ResultUtils.success(res);
    }

    /**
     * 保存。
     *
     * @param userAddRequest
     * @return {@code true} 保存成功，{@code false} 保存失败
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("add")
    public BaseResponse<Long> save(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest==null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userAddRequest,user);
        String password="123rain";
        String encryptPassword = userService.getEncryptPassword(password);
        user.setUserPassword(encryptPassword);
        boolean save = userService.save(user);
        ThrowUtils.throwIf(!save, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(user.getId());
    }

    /**
     * 根据主键删除。
     *
     * @param deleteRequest 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest==null ||
                deleteRequest.getId()==null, ErrorCode.PARAMS_ERROR);
        boolean res = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(res);
    }

    /**
     * 根据主键更新。
     *
     * @param userUpdateRequest
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest==null ||
                userUpdateRequest.getId()==null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest,user);
        boolean res = userService.updateById(user);
        ThrowUtils.throwIf(!res, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(res);
    }

    /**
     * 根据主键获取。
     *
     * @param userId 主键
     * @return 详情
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @GetMapping("get")
    public BaseResponse<User> getUser(long userId) {
       ThrowUtils.throwIf(userId<0, ErrorCode.PARAMS_ERROR);
        User byId = userService.getById(userId);
        ThrowUtils.throwIf(byId==null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(byId);
    }
    @GetMapping("get/vo")
    public BaseResponse<UserVo> getUserVo(long userId) {
        ThrowUtils.throwIf(userId<0, ErrorCode.PARAMS_ERROR);
        User byId = userService.getById(userId);
        ThrowUtils.throwIf(byId==null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.getUserVo(byId));
    }

    /**
     * 分页查询。
     *
     * @param userQueryRequest 分页对象
     * @return 分页对象
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("list/page/vo")
    public BaseResponse<Page<UserVo>> page(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest==null, ErrorCode.PARAMS_ERROR);
        int pageNum = userQueryRequest.getPageNum();
        int pageSize = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(pageNum, pageSize)
                , userService.getQueryWrapper(userQueryRequest));
        List<User> pageRecords = userPage.getRecords();
        List<UserVo> userVos = pageRecords.stream()
                .map(userService::getUserVo).collect(Collectors.toList());
        Page<UserVo> userVoPage = new Page<>(pageNum,pageSize);
        userVoPage.setRecords(userVos);
        return ResultUtils.success(userVoPage);
    }

}
